/**
 * 
 * @authors Riche
 * @date    2016-07-06 14:39:22
 * @version 1.1.0
 */
var commentID = $.getUrlVar("id");
var answerID = $.getUrlVar("aid");

var vm = new Vue({
    el: "#app",
    data: {
        title: '',
        typeID: '',
        addShow: false,
        addphotoshow: false,
        blocks: [],
        askTitle: '',
        askContent: '',
        contentPicture: []
    },
    compiled: function() {
        var _self = this;
        _self.loadPhoto(); //调用图片预加载


        Common.ajaxCall({ url: "ask_getTypeList", datatype: 'json', type: 'get' }, function(data) {
            if (data.error_code == 0) {
                _self.blocks = data.data;
            } else {
                alert(data.msg);
            }
        });

        // 选择图片
        /*
        document.getElementById('pic').onchange = function() {
            var img = event.target.files[0];
            // 判断是否图片  
            if (!img) {
                return;
            }
            // 判断图片格式  
            if (!(img.type.indexOf('image') == 0 && img.type && /\.(?:jpg|png|gif)$/.test(img.name))) {
                alert('图片只能是jpg,gif,png');
                return;
            }

            var data = {
                pic: new FormData(img),
            };
            //var data = new FormData();
            //data.append('pic', img);
            Common.ajaxSubmitPic({ target: "#am_form", url: 'common_uploadPic', datatype: 'json', type: 'post', data: data }, function(data) {
                if (data.error_code == 0) {
                    var dom = '<li class="imgBox j_1" ><i class="closeImg" onclick="delImg(this);"></i><img src="' + data.msg + '" id="pic1" class="loadImg"></li>';
                    $('#dom').append(dom);
                    //$('#pic1').attr('src',image);
                    _self.contentPicture.push(data.msg);
                    myJS.imgScale('.j_1', '1', '1');
                    myJS.ImgFull('.loadImg', 'images/liby.png');
                    //vm.addReplyPic(image);
                    $("#pic").val('');
                } else {
                    alert(data.msg);
                }
            });
        }
        */
        // 选择图片
        document.getElementById('pic').onchange = function (e) {
            event = e || event;
            var img = event.target.files[0];
            var formData = new FormData();
            if (!img) {
                return;
            }
            // 判断图片格式  
            if (!(img.type.indexOf('image') == 0 && img.type && /\.(?:jpg|png|gif)$/.test(img.name))) {
                alert('图片只能是jpg,gif,png');
                return;
            }

            //定义图片参数
            var uploadFile = new FormData();
            uploadFile.append("uploadFile", img);
            //var uploadFile = new FormData(img);

            //接口方面的更新，现在上传图片必须得有accessToken等
            getaccessToken(function(token) {

                var url = 'ask_getFolderId';
                Common.ajaxCall({
                    url: url,
                    datatype: 'json',
                    type: 'post'
                }, function(data) {
                    if (data.error_code == 0) {
                        uploadFile.append("folderId", data.data[0].folderId);
                        uploadFile.append("accessToken", token);
                        uploadFile.append("tags", _self.title);

                        //data = Common.jsonEncode(data);
                        Common.ajaxSubmitPic({
                            url: 'upload_file',
                            data: uploadFile,
                        }, function(data) {
                            if (data.error_code == 0) {
                                
                                var dom = '<li class="imgBox j_1" ><i class="closeImg" onclick="delImg(this);"></i><span class="photo j_1"><img data-src="' + data.data[0].content + '" data-fileid="' + data.data[0].fileId + '"  class="loadImg"></span></li>';
                                $('#dom').append(dom);
                                //$('#pic1').attr('src',image);
                                _self.contentPicture.push(data.msg);
                                _self.loadPhoto();
                                //vm.addReplyPic(image);
                                /*
                                //添加图片到内容中去
                                $('#content').htmlarea('tofocus');
                                $('#content').htmlarea('image', data.data[0].content);*/
                            } else {
                                alert(data.msg);
                            }
                        });
                    } else {
                        alert(data.msg);
                    }
                });
            });
        }
    },
    methods: {
        fn: function(flag) {
            switch (flag) {
                case 'level':
                    this.willShow = flag;
                    this.title = '用户星级排行榜';

                    break;
                case 'exchange':
                    this.willShow = flag;
                    this.title = '最近兑换商品';
                    break;
                case 'record':
                    this.willShow = flag;
                    this.title = '白金币记录';

                    break;
                case 'vault':
                    this.willShow = flag;
                    this.title = '我的金库';
                    break;
            }
        },
        publish: function() { //提交提问内容
            var _self = this;
            if (!_self.typeID) {
                alert('请选择板块');
                return false;
            }
            if (!(_self.typeID && _self.askTitle.trim())) { //如果没填写内容
                alert('请填写标题');
                $('#askTitle').focus();
                return false;
            }
            var content = $("#content").val();
            //content = $('#content').htmlarea('html'); //取内容

            var data = {
                typeID: _self.typeID,
                title: _self.askTitle,
                content: content,
                contentPicture: _self.contentPicture.join(',')
            };
            //data = Common.jsonEncode(data);
            Common.ajaxCall({ url: 'ask_addAsk', datatype: 'json', type: 'get', data: data }, function(data) {
                if (data.error_code == 0) {
                    alert(data.msg);
                    setTimeout(function() { //延迟跳转
                        window.location.href = "asking.html";
                    }, 500);
                } else {
                    alert(data.msg);
                }
                $('#pic').val();
            });

        },
        addphoto: function() {
            if (this.addphotoshow) {
                this.addphotoshow = false;
            } else {
                this.addphotoshow = true;
            }
        },
        loadPhoto: function() {
            myJS.imgScale('.j_1', '1', '1');
            myJS.imgScale('.j_1511', '15', '11');
            myJS.ImgFull('.loadImg', 'images/liby.png');
        },
        selectPic: function() {
            //选择图片
            $("#pic").click();
        }
    },
    watch: {
        "blocks": function() {
            var _self = this;
            //初始化mobilescroll组件
            var opt = {
                'default': {
                    theme: 'default',
                    mode: 'scroller',
                    display: 'bottom',
                    animate: 'fade',
                    placeholder: '选择板块',
                    //交换按钮位置
                    buttons: [
                        'set'
                    ],

                    headerText: function(valueText) { return "选择板块"; },
                    // 选择之后赋值
                    onSelect: function(e) {

                        _self.title = $('#names_dummy').val();
                    }

                },
                'select-opt': {
                    preset: 'select',
                    group: false
                }
            }
            $('.block-select-opt').scroller($.extend(opt['select-opt'], opt['default']));
            _self.typeID = $('#names').children('option').eq(0).val();
            $('#names').change(function() {
                _self.typeID = $(this).children('option:selected').val(); //拿到value值
            });
            _self.title = $('#names_dummy').val(); //初始化赋值

        }
    }
});